package com.leyu.mall.admin.cms.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.leyu.mall.domain.cms.CmsSlide;
import com.leyu.mall.service.cms.ICmsSlideService;
/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/slide")
public class CmsSlideController
{
	@Resource(name = "cmsSlideServiceImpl")
	private ICmsSlideService	cmsSlideServiceImpl;

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, CmsSlide cmsSlide)
	{
		try
		{
			cmsSlideServiceImpl.save(cmsSlide);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../slide";
	}

	@RequestMapping(value = "delete/{slideId}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long slideId)
	{
		try
		{
			cmsSlideServiceImpl.deleteCmsSlide(slideId);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../slide";
	}

	@RequestMapping(value = "edit/{slideId}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long slideId)
	{
		try
		{
			model.addAttribute("entity", cmsSlideServiceImpl.getCmsSlideById(slideId));
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editslide.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request)
	{
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newADLink(Model model) throws Exception
	{
		return "admin/newslide.html";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model) throws Exception
	{
		try
		{
			model.addAttribute("list", cmsSlideServiceImpl.getAllCmsSlide());
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/slide.html";
	}
}
